import Vue from 'vue'
import Vuex from 'vuex'
import api from "@/api/api"
import MinCache from'@/common/util/MinCache.js'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: '',
    userid:'',
    username: '',
    shortName: '',
    mobile: '',
    avatar: '',
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, { username, userId, shortName, mobile }) => {
      state.username = username
      state.userId = userId
      state.shortName = shortName
      state.mobile = mobile
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    }
  },
  actions: {
    // 登录
    mLogin({ commit }, userInfo) {
      console.log("mLogin",userInfo)
      return new Promise((resolve, reject) => {
       api.login(userInfo).then(response => {
		   console.log(response.data);
          if(response.data.errcode ==0){ 
            const result = response.data.data
            // const userInfo = result.userInfo
			console.log(result);
			uni.setStorageSync('token',result.token);
			uni.setStorageSync('userName',result.userName);
            commit('SET_TOKEN', result.token)
            commit('SET_NAME', { username: result.userName,userId: userInfo.userId, shortName:userInfo.shortName, mobile:userInfo.mobile})
            resolve(response.data)
          }else{
            resolve(response.data)
          }
        }).catch(error => {
			console.log("catch===>response",response)
          reject(error)
        })
      })
    },
	// 扫一扫 通过loginCode登录
	loginByLoginCode({ commit }, userInfo) {
	  console.log("loginByLoginCode",userInfo)
	  return new Promise((resolve, reject) => {
	   api.loginByQr(userInfo).then(response => {
		   console.log(response.data);
	      if(response.data.errcode ==0){ 
	        const result = response.data.data
	        // const userInfo = result.userInfo
			console.log(result);
			uni.setStorageSync('token',result.token);
			uni.setStorageSync('userName',result.userName);
	        commit('SET_TOKEN', result.token)
	        commit('SET_NAME', { username: result.userName,userId: userInfo.userId, shortName:userInfo.shortName, mobile:userInfo.mobile})
	        resolve(response.data)
	      }else{
	        resolve(response.data)
	      }
	    }).catch(error => {
			console.log("catch===>response",response)
	      reject(error)
	    })
	  })
	},
    //手机号登录
    PhoneLogin({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        api.phoneNoLogin(userInfo).then(response => {
			// console.log(response.data);
          if(response.data.code =='200'){
            const result = response.data.result
            const userInfo = result.userInfo
            commit('SET_TOKEN', result.token)
            commit('SET_NAME', { username: userInfo.username,realname: userInfo.realname, phone: userInfo.phone})
            commit('SET_AVATAR', userInfo.avatar)
            resolve(response.data)
          }else{
            reject(response.data)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 登出
    Logout({ commit, state }) {
      return new Promise((resolve) => {
        let logoutToken = state.token;
        commit('SET_TOKEN', '')
        uni.removeStorageSync('token')
        api.logout(logoutToken).then(() => {
          resolve()
        }).catch(() => {
          resolve()
        })
      })
    },

  },
  getters:{
    token: state => state.token,
	username: state => {state.username=uni.getStorageSync(USER_INFO).username; return state.username},
	nickname: state => {state.realname=uni.getStorageSync(USER_INFO).realname; return state.realname},
	avatar: state => {state.userid=uni.getStorageSync(USER_INFO).avatar; return state.avatar},
	userid:state => {state.userid=uni.getStorageSync(USER_INFO).id; return state.userid},
	phone:state => {state.phone=uni.getStorageSync(USER_INFO).phone; return state.phone},
  }
})
